﻿/********************************************************
 * 类名： InitPayBLL.cs
 * 功能： 付款单管理业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2014-02-08	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using PSS.DAL.FundManage;
using PSS.Entity.FundManage;
using PSS.Utility;

namespace PSS.BLL.FundManage
{
    public class InitPayBLL
    {
        private readonly InitPayDAL _dal = new InitPayDAL();

        public string GetInitPayList(InitPayQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetInitPayList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetInitPayInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetInitPayInfo(id));
        }

        public string GetInitPayDetailList(InitPayDetailQcEntity qcmodel)
        {
            var list = _dal.GetInitPayDetailList(qcmodel);
            return JsonHelper.Encode(list);
        }

        public void AddInitPay(string data)
        {
            var model = new InitPayEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<InitPayDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.InitPayId = Guid.NewGuid();
                model.VendorId = new Guid(row["VendorId"].ToString());
                model.SinglePaymentDate = Convert.ToDateTime(row["SinglePaymentDate"].ToString());
                model.SinglePaymentNo = row["SinglePaymentNo"].ToString();
                model.Remark = row["Remark"] != null ? row["Remark"].ToString() : string.Empty;
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {

                    var cmodel = new InitPayDetailEntity
                    {
                        PayDetailId = Guid.NewGuid(),
                        InitPayId = model.InitPayId,
                        AccountId = new Guid(item["AccountId"].ToString()),
                        PayMoney =
                            item["PayMoney"] != null
                                ? Convert.ToDecimal(item["PayMoney"])
                                : 0,
                        SCategoryId = new Guid(item["SCategoryId"].ToString()),
                        SettleNo = item["SettleNo"].ToString(),
                        Remark = item["Remark"].ToString()
                    };
                    list.Add(cmodel);
                }
            }
            if (!_dal.AddInitPay(model)) return;
            _dal.AddInitPayDetail(list);
        }

        public void ModifyInitPay(string data)
        {
            var model = new InitPayEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<InitPayDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.InitPayId = new Guid(row["InitPayId"].ToString());
                model.VendorId = new Guid(row["VendorId"].ToString());
                model.SinglePaymentDate = Convert.ToDateTime(row["SinglePaymentDate"].ToString());
                model.SinglePaymentNo = row["SinglePaymentNo"].ToString();
                model.Remark = row["Remark"] != null ? row["Remark"].ToString() : string.Empty;
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {

                    var cmodel = new InitPayDetailEntity
                    {
                        PayDetailId = Guid.NewGuid(),
                        InitPayId = model.InitPayId,
                        AccountId = new Guid(item["AccountId"].ToString()),
                        PayMoney =
                            item["PayMoney"] != null
                                ? Convert.ToDecimal(item["PayMoney"])
                                : 0,
                        SCategoryId = new Guid(item["SCategoryId"].ToString()),
                        SettleNo = item["SettleNo"].ToString(),
                        Remark = item["Remark"].ToString()
                    };
                    list.Add(cmodel);
                }
            }
            if (!_dal.ModifyInitPay(model)) return;

            _dal.AddInitPayDetail(list);
        }

        public bool RemoveInitPay(string id)
        {
            _dal.RemoveInitPayDetail(id);
            return _dal.RemoveInitPay(id);
        }
    }
}
